package com.example.survey23.controller;


import com.example.survey23.common.lang.Result;
import com.example.survey23.entity.AnswerBankGap;
import com.example.survey23.entity.AnswerBankMultiple;
import com.example.survey23.entity.AnswerBankRadio;
import com.example.survey23.entity.QuestionBankProblem;
import com.example.survey23.service.QuestionBankProblemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 23Group
 * @since 2022-11-10
 */
@RestController
@RequestMapping("/question-bank-problem")
@Slf4j
public class QuestionBankProblemController {
    @Autowired
    QuestionBankProblemService questionBankProblemService;
    @Autowired
    AnswerBankRadioController answerBankRadioController;
    @Autowired
    AnswerBankMultipleController answerBankMultipleController;
    @Autowired
    AnswerBankGapController answerBankGapController;
    // 保存题目
    @PostMapping("/saveQuestion")
    public Result saveQuestion(@RequestBody HashMap map){
        Integer  belongId = (Integer) map.get("belongId");
        List<HashMap> list = (List<HashMap>) map.get("modelList");

        // 保存问题
        for(HashMap curMap: list){
            // 得到题目类型
            int modelType = (Integer) curMap.get("modelType");
            // 得到题目体
            String subject = (String)curMap.get("subject");
            // 得到题目选项
            List<LinkedHashMap> listO = (List<LinkedHashMap>) curMap.get("option");
            log.info(listO + "");
            int id = questionBankProblemService.getMaxSurveyBaseId();
            QuestionBankProblem questionBankProblem = new QuestionBankProblem();
            questionBankProblem.setId(id+1);
            questionBankProblem.setBelongId(belongId);
            questionBankProblem.setQuName(subject);
            questionBankProblem.setQuType(modelType);
            // 保存题目
            questionBankProblemService.save(questionBankProblem);
            log.info("题目id 为 ：" + questionBankProblem.getId() + "");
            if(modelType == 1){
                log.info("进入单选保存部分");
                List<AnswerBankRadio> listR = new ArrayList<>();
                int Rid = answerBankRadioController.getMaxRId() + 1;
                for(int i=0; i<listO.size(); i++){
                    log.info("进入单选构造循环");
                    AnswerBankRadio answerBankRadio = new AnswerBankRadio();
                    HashMap mapO = listO.get(i);
                    answerBankRadio.setOptionName((String) mapO.get("value"));
                    answerBankRadio.setProId(questionBankProblem.getId());
                    answerBankRadio.setId(Rid);
                    answerBankRadioController.save(answerBankRadio);
                    Rid++;
                }
            }else if(modelType == 2){
                List<AnswerBankMultiple> listM = new ArrayList<>();
                int IdM = answerBankMultipleController.getMaxMId() + 1;
                for(int i=0; i<listO.size(); i++){
                    AnswerBankMultiple answerBankMultiple = new AnswerBankMultiple();
                    HashMap mapO = listO.get(i);
                    answerBankMultiple.setId(IdM);
                    answerBankMultiple.setOptionName((String)mapO.get("value"));
                    answerBankMultiple.setProId(belongId);
                    answerBankMultipleController.save(answerBankMultiple);
                    IdM++;
                }

            }else if(modelType == 3){
                int IdG = answerBankGapController.getMaxGId() + 1;
                for(int i=0; i<listO.size(); i++){
                    AnswerBankGap answerBankGap = new AnswerBankGap();
                    answerBankGap.setId(IdG);
                    answerBankGap.setProId(belongId);
                    answerBankGapController.save(answerBankGap);
                    IdG++;
                }
            }
        }

        return Result.succ(null);
    }

}
